/**
 * 附件处理
 */
(function ($) {
    $.fn.uploadAttached = function (mark, opts) {
        var btn = $(this).attr("id");
        switch (mark) {
            case "image":
                uploadImg(btn, mark, opts);return;
            case "insertfile":
                uploadFile(btn, mark, opts);return;
            case "media":
                uploadMedia(btn, mark, opts);return;
            case "resource":
                uploadResource(btn, opts);return;
        }
    };
    $.fn.loadResource = function (opts) {
        _$o = $(this);
        $.post(opts.url, function (response) {
            $.each(response, function (i, o) {
                addThumbnail(_$o, o.resourceAddress, o.resourceName, o.resourceSuffix, o.resourceSize);
            })
        })
    };
})(jQuery);

window.uploadImg = function (btn, mark, opts) {
    KindEditor.ready(function (K) {
        var editor = K.editor();
        K("#" + btn + "").click(function () {
            editor.loadPlugin("" + mark + "", function () {
                editor.plugin.imageDialog({
                    clickFn: function (url, title, suffix, size) {
                        $("#" + opts.name + "").val(title);
                        $("#" + opts.url + "").val(url);
                        $("#" + opts.suffix + "").val(suffix);
                        $("#" + opts.size + "").val(size);
                        $("#" + opts.attached + "").show();
                        $("#" + opts.attached + "").attr("src", url);
                        editor.hideDialog();
                    }
                });
            });
        });
    });
    showAttached(opts.url, opts.attached);
};
window.showAttached = function (imgUrl, imgSrc) {
    var imgPath = $("#" + imgUrl + "").val();
    if (imgPath != "") {
        $("#" + imgSrc + "").show();
        $("#" + imgSrc + "").attr("src", imgPath);
    }
};

window.uploadFile = function (btn, mark, opts) {
    KindEditor.ready(function (K) {
        var editor = K.editor();
        K("#" + btn + "").click(function () {
            editor.loadPlugin("" + mark + "", function () {
                editor.plugin.fileDialog({
                    clickFn: function (url, title, suffix, size) {
                        $("#" + opts.name + "").val(title);
                        $("#" + opts.url + "").val(url);
                        $("#" + opts.suffix + "").val(suffix);
                        $("#" + opts.size + "").val(size);
                        $("#" + opts.title + "").text(title);
                        $("#" + opts.attached + "").show();
                        editor.hideDialog();
                    }
                });
            });
        });
    });
};

var uploadMedia = function (btn, mark, opts) {
    KindEditor.ready(function (K) {
        var editor = K.editor();
        K("#" + btn + "").click(function () {
            editor.loadPlugin("" + mark + "", function () {
                editor.plugin.media.edit({
                    clickFn: function (url, title, suffix, size) {
                        $("#" + opts.name + "").val(title);
                        $("#" + opts.url + "").val(url);
                        $("#" + opts.suffix + "").val(suffix);
                        $("#" + opts.size + "").val(size);
                        $("#" + opts.title + "").text(title);
                        $("#" + opts.attached + "").show();
                        editor.hideDialog();
                    }
                });
            });
        });
    });
};
window.uploadResource = function (btn, opts) {
    KindEditor.ready(function (K) {
        var editor = K.editor();
        K("#" + btn + "").click(function () {
            editor.loadPlugin("insertfile", function () {
                editor.plugin.fileDialog({
                    clickFn: function (url, title, suffix, size) {
                        $.post(opts.saveUrl, $.toJSON({
                                parentId: opts.parentId,
                                resourceName: title,
                                resourceAddress: url,
                                resourceSuffix: suffix,
                                resourceSize: size
                            }),
                            function () {
                                addThumbnail($("#scrollTable"), url, title, suffix, size);
                            }
                        )
                        editor.hideDialog();
                    }
                });
            });
        });
    });
};
function addThumbnail(_$object, url, title, suffix, size) {
    var fileicon;
    if (suffix == "jpg") {
        fileicon = $("<img/>").addClass("file-icon").addClass("file-icon-img").attr("src", url);
    } else {
        fileicon = $("<div/>").addClass("file-icon").addClass("file-icon-" + suffix);
    }
    var filetitle = $("<div/>").addClass("file-title").html(title);
    var fileitem = $("<div/>").addClass("file-item").append(fileicon).append(filetitle);
    _$object.append(fileitem);
}
